<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay as LibraryPay;
use app\common\model\Bank;

/**
 * 支付接口
 */
class Pay extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 支付
     * @ApiMethod (POST)
     * @param int $id  订单id
     * @param string $type  订单类型（wm=外卖，er=二手商城）
     * @param string $email  邮箱
     * @param string $pay_type  支付方式（stripe=信用卡）
     * @param int $bank_id  信用卡ID
     * 
     * 
     */
    public function pay()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $post = $this->request->post();
        $userInfo = $this->auth->getUser();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        if (!isset($pay_type)) {
            $this->error('请选择支付方式');
        }
        if ($pay_type == 'stripe') {
            if (!isset($bank_id) || empty($bank_id)) {
                $this->error('请选择银行卡');
            }
            $stripe_info = Bank::get($bank_id);
            $stripe_info->email = $email;
        } else {
            $stripe_info = [];
        }
        try {
            $data = LibraryPay::pay($id, $type, $userInfo, $pay_type, $stripe_info);
        } catch (\Exception $e) {
            $this->error('提交支付失败：' . $e->getMessage());
        }
        $this->success('', $data ?? '');
    }
}
